// ----------------------------------------------------------------------------
//  This code is taken from:
//      http://blog.paranoidferret.com/index.php/2008/01/30/csharp-tutorial-how-to-use-custom-cursors/
//
using System;
using System.Drawing;
using System.Windows;
using System.Runtime.InteropServices;

namespace NUtils
{
    public static class Cursor
    {
        private struct IconInfo
        {
            public bool fIcon;
            public int xHotspot;
            public int yHotspot;
            public IntPtr hbmMask;
            public IntPtr hbmColor;
        }

        [DllImport("user32.dll")]
        private static extern IntPtr CreateIconIndirect(
            ref IconInfo icon);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GetIconInfo(IntPtr hIcon,
            ref IconInfo pIconInfo);

        // CreateCursor
        //  Unfortunately, .Net doesn't provide a mechanism to override the
        //  HotSpot on a Cursor's Icon. I've take this code from an example on
        //  the internet that allows you to create a cursor with a hotspot 
        //  at any location on the icon.
        public static System.Windows.Forms.Cursor CreateCursor(IntPtr hIcon, int xHotSpot, int yHotSpot)
        {
            IconInfo tmp = new IconInfo();
            GetIconInfo(hIcon, ref tmp);
            tmp.xHotspot = xHotSpot;
            tmp.yHotspot = yHotSpot;
            tmp.fIcon = false;
            return new System.Windows.Forms.Cursor(CreateIconIndirect(ref tmp));
        }
    }
}